package com.scmt.hmzd.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmCoupon;
import com.scmt.hmzd.entity.HmCouponUser;
import com.scmt.hmzd.mapper.HmCouponUserMapper;
import com.scmt.hmzd.service.IHmCouponService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmCouponMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.text.SimpleDateFormat;

/**
 * @author
 **/
@Service
public class HmCouponServiceImpl extends ServiceImpl<HmCouponMapper, HmCoupon> implements IHmCouponService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmCouponMapper hmCouponMapper;

    @Autowired
    private HmCouponUserMapper hmCouponUserMapper;

    @Override
    public IPage<HmCoupon> queryHmCouponListByPage(HmCoupon hmCoupon, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmCoupon> pageData = new Page<>(page, limit);
        QueryWrapper<HmCoupon> queryWrapper = new QueryWrapper<>();
        if (hmCoupon != null) {
            queryWrapper = LikeAllField(hmCoupon, searchVo);
        }
        IPage<HmCoupon> result = hmCouponMapper.selectPage(pageData, queryWrapper);
        for (HmCoupon record : result.getRecords()) {
            Integer useNum = hmCouponMapper.countUseNum(record.getCouponId());
            Integer takeNum = hmCouponMapper.countTakeNum(record.getCouponId());
            record.setUseNum(useNum != null ? useNum : 0);
            record.setTakeNum(takeNum != null ? takeNum : 0);
        }
        return result;
    }

    @Override
    public Map<String, Long> getCouponCountByStatus(String userId) {
        return hmCouponMapper.getCouponCountByStatus(userId);
    }

    @Override
    public IPage<HmCoupon> getCouponListByStatus(PageVo pageVo, String userId, Integer status) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmCoupon> pageData = new Page<>(page, limit);
        IPage<HmCoupon> result = hmCouponMapper.getCouponListByStatus(pageData, userId, status);
        return result;
    }

    @Override
    public void receive(HmCoupon coupon, String userId) {
        Date nowTime = new Date();
        HmCouponUser couponUser = new HmCouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponId(coupon.getCouponId());
        couponUser.setStatus(1);
        couponUser.setReceiveTime(nowTime);
        couponUser.setUserStartTime(nowTime);
        couponUser.setUserEndTime(DateUtils.addDays(couponUser.getUserStartTime(), coupon.getValidDays() + 1));
        couponUser.setIsDelete(0);
        hmCouponUserMapper.insert(couponUser);
    }

    @Override
    public IPage<HmCoupon> getCouponList(PageVo pageVo, String userId) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmCoupon> pageData = new Page<>(page, limit);
        IPage<HmCoupon> result = hmCouponMapper.getCouponList(pageData, userId);
        return result;
    }


    /**
     * 功能描述：构建模糊查询
     *
     * @param hmCoupon 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmCoupon> LikeAllField(HmCoupon hmCoupon, SearchVo searchVo) {
        QueryWrapper<HmCoupon> queryWrapper = new QueryWrapper<>();
        if (hmCoupon.getCouponId() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.coupon_id", hmCoupon.getCouponId()));
        }
        if (StringUtils.isNotBlank(hmCoupon.getCouponName())) {
            queryWrapper.and(i -> i.like("hm_coupon.coupon_name", hmCoupon.getCouponName()));
        }
        if (StringUtils.isNotBlank(hmCoupon.getSubTitle())) {
            queryWrapper.and(i -> i.like("hm_coupon.sub_title", hmCoupon.getSubTitle()));
        }

        if (hmCoupon.getCashCondition() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.cash_condition", hmCoupon.getCashCondition()));
        }
        if (hmCoupon.getReduceAmount() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.reduce_amount", hmCoupon.getReduceAmount()));
        }
        if (hmCoupon.getAfterReceiveDays() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.after_receive_days", hmCoupon.getAfterReceiveDays()));
        }
        if (hmCoupon.getValidDays() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.valid_days", hmCoupon.getValidDays()));
        }
        if (hmCoupon.getStocks() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.stocks", hmCoupon.getStocks()));
        }
        if (hmCoupon.getLimitNum() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.limit_num", hmCoupon.getLimitNum()));
        }
        if (hmCoupon.getOverdueStatus() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.overdue_status", hmCoupon.getOverdueStatus()));
        }
        if (hmCoupon.getSourceStock() != null) {
            queryWrapper.and(i -> i.like("hm_coupon.source_stock", hmCoupon.getSourceStock()));
        }
        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmCoupon::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        return queryWrapper;
    }
}
